package com.dingwen.treasure.business.controller;

import com.dingwen.treasure.base.controller.BaseViewController;
import com.dingwen.treasure.business.component.event.CreateOrderEvent;
import com.dingwen.treasure.business.component.event.CreateOrderEvent2;
import com.dingwen.treasure.business.model.dto.OrderDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 订单控制器: 观察者模式  </br>
 * <p>模拟创建订单业务：指定库存、物流业务逻辑</p>
 *
 * @author dingwen
 * @date 2022/04/23
 */
@Api(tags = "订单控制器,观察者模式")
@RestController
@RequestMapping("order")
@Slf4j
public class OrderController implements BaseViewController {

    /**
     * 应用程序上下文
     */
    @Resource
    private  ApplicationContext applicationContext;

    /**
     * 下单
     */
    @ApiOperation(value = "创建订单")
    @ApiImplicitParam(name = "orderDTO",value = "订单DTO", required = true, paramType = "body")
    @PostMapping
    public void createOrder(@RequestBody @Valid OrderDto orderDTO) {
        log.info("当前线程：{}正在创建订单", Thread.currentThread().getName());
        applicationContext.publishEvent(new CreateOrderEvent(orderDTO));
    }

    @ApiOperation(value = "创建订单2")
    @ApiImplicitParam(name = "orderDTO",value = "订单DTO", required = true, paramType = "body")
    @PostMapping("api2")
    public void createOrder2(@RequestBody @Valid OrderDto orderDTO){
        log.info("当前线程：{}正在创建订单", Thread.currentThread().getName());
        applicationContext.publishEvent(new CreateOrderEvent2(orderDTO));
    }
}
